//
//  SEGHelpManager.m
//  ExcellentHome
//
//  Created by 刘山 on 16/8/23.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGHelpManager.h"

@implementation SEGHelpManager
/**
 * 获取帮帮发布时的分类
 * @author Wendy
 */
- (void)requestPublishHelpClassify:(SEGRequestCallBack)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/quizTypeList.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {

        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        NSArray* list = [dicData valueForKey:@"quizTypeList"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        self.quizTypeList = [list copy];
        
        finished(isSuccessed, codeType, errorMessage);
        
    }];
}

#pragma mark - 根据问题类型获取问题列表
- (void)requestFindQuizListByQuizTypeId:(NSString*)quizTypeId
                            quizRangeId:(NSString*)quizRangeId
                               regionId:(NSString*)regionId
                                organId:(NSString*)organId
                                 pageNo:(NSInteger)pageNo
                               pageSize:(NSInteger)pageSize
                               Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
                             @"quizTypeId" : ValueToString(quizTypeId),
                             @"quizRangeId" : ValueToString(quizRangeId),
                             @"regionId" : ValueToString(regionId),
                             @"organId" : ValueToString(organId),
                             @"pageNo" : @(pageNo),
                             @"pageSize" : @(pageSize),
                             @"userId" : kAccountUserID
                             };
    
    __block NSInteger _pageNo = pageNo;
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/findQuizListByQuizType.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([result valueForKey:@"data"] && [[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary* dicData = [result valueForKey:@"data"];
            
            NSArray* quizList = [dicData valueForKey:@"quizList"];
            
            if (_pageNo != 1 && quizList && quizList.count <= 0) {
                finished(NO, codeType, @"没有更多的数据！"); //数据返回正常，但是数据为空
                return;
            }
            
            NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:quizList.count];
            
            NSString *userIds = @"";
            
            for (NSDictionary* dic in quizList) {
                
                SEGFindQuizListByQuizTypeModel* _model = [SEGFindQuizListByQuizTypeModel initWithDictionary:dic];
                
                _model.quizContent1 = [_model.quizContent copy];
                
                [arrM addObject:_model];
                
                userIds = [userIds stringByAppendingString:[NSString stringWithFormat:@",%@",_model.userId]];
            }
            
            if (_pageNo == 1) { //顶部刷新
                self.dataSourceFindQuizList = @[];
            }
            
            [self FindQuizListLogic:arrM];
            
            NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.dataSourceFindQuizList];
            [_tmpData addObjectsFromArray:arrM];
            
            //防止内容太多造成内容满的问题
            if (_tmpData.count > 100) {
                [_tmpData removeObjectsInRange:NSMakeRange(0, 50)];
            }
            
            self.dataSourceFindQuizList = [_tmpData copy];
            
            
            if ([userIds length] > 0) {
                userIds = [userIds substringFromIndex:1];
            }
            
            
            finished(YES, codeType, userIds);
            return;
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

//帮帮内容显示逻辑整理
- (void)FindQuizListLogic:(NSArray<SEGFindQuizListByQuizTypeModel*>*)arr
{
    [arr enumerateObjectsUsingBlock:^(SEGFindQuizListByQuizTypeModel* _Nonnull obj, NSUInteger idx, BOOL* _Nonnull stop) {
        
        if ([obj.objType isEqualToString:@"5"]) { // 话题
            obj.LinkType = SEGFindQuizListByQuizTypeModelLinkTypeTOPIC;
            if ([obj.linkTitle length] > 0) {
                obj.linkTitle = [NSString stringWithFormat:@"#%@#", obj.linkTitle];
                obj.quizContent = [NSString stringWithFormat:@"%@ %@", obj.linkTitle, obj.quizContent];
            }
        }
        else if ([obj.objType isEqualToString:@"11"]) { // 帮帮中的赞赞
            obj.LinkType = SEGFindQuizListByQuizTypeModelLinkTypePRAISE;
            obj.userName = @"帮帮正能量";
            obj.communityName = [NSString stringWithFormat:@"来自 %@",kAppName];
            obj.isAuth = @(3);
            obj.linkTitle = @"#物业服务#";
            obj.quizContent = [NSString stringWithFormat:@"%@ %@\n点击查看奖状", obj.linkTitle, obj.quizContent];
        }
        else if ([obj.linkTitle length] > 0 && [obj.linkUrl length] > 0) { //原文链接
            obj.LinkType = SEGFindQuizListByQuizTypeModelLinkTypeLINK;
            obj.quizContent = [NSString stringWithFormat:@"原文 %@", obj.quizContent];
        }
        else { //原文无链接
            obj.LinkType = SEGFindQuizListByQuizTypeModelLinkTypeNO;
        }
    }];
}

#pragma mark - 帮帮 我的展示列表
- (void)requestFindMeQuizListFinished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
                             @"userId" : kAccountUserID
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/findMeQuizList.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([result valueForKey:@"data"] && [[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary* dicData = [result valueForKey:@"data"];
            
            self.dataSourceFindMeQuizList = [SEGFindMeQuizListModel initWithDictionary:dicData];
            
            finished(YES, codeType, errorMessage);
            return;
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

- (void)requestQuizDetailByQuizId:(NSString*)quizId Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
                             @"quizId" : quizId,
                             @"userId" : kAccountUserID
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/quizDetail.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([result valueForKey:@"data"] && [[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary* dicData = [result valueForKey:@"data"];
            
            if (!([dicData valueForKey:@"quizDto"] && [[dicData valueForKey:@"quizDto"] isKindOfClass:[NSDictionary class]])) {
                finished(NO, codeType, @"没有话题数据！");
                return;
            }
            
            NSDictionary* data = [dicData valueForKey:@"quizDto"];
            
            
            NSArray<SEGFindQuizListByQuizTypeModel*>* _tmp = @[ [SEGFindQuizListByQuizTypeModel initWithDictionary:data] ];
            
            _tmp.firstObject.quizContent1 = [_tmp.firstObject.quizContent copy];
            
            [self FindQuizListLogic:_tmp];
            
            self.dataSourceQuizDetail = _tmp.firstObject;
            
            // 通过传递userId来请求相关的积分
            finished(YES, codeType, self.dataSourceQuizDetail.userId);
            return;
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

- (void)requestQuizAnswListByQuizId:(NSString*)quizId pageNo:(NSInteger)pageNo pageSize:(NSInteger)pageSize objType:(NSString*)objType Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{
                             @"quizId" : quizId,
                             @"pageNo" : @(pageNo),
                             @"pageSize" : @(pageSize),
                             @"userId" : kAccountUserID
                             //@"objType" : objType 废弃不要了
                             };
    
    __block NSInteger _pageNo = pageNo;
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/quizAnswList.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }
        
        if ([result valueForKey:@"data"] && [[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary* dicData = [result valueForKey:@"data"];
            NSArray* data = [dicData valueForKey:@"answList"];
            
            if (_pageNo != 1 && data && data.count == 0) {
                finished(NO, codeType, @"没有更多的数据！"); //数据返回正常，但是数据为空
                return;
            }
            
            NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:data.count];
            
            NSString* userIds = @"";
            for (NSDictionary* dic in data) {
                SEGQuizAnswListModel* _model = [SEGQuizAnswListModel initWithDictionary:dic];
                _model.answContent1 = [_model.answContent copy];
                if (_model.byReviewNick.length > 0) {
                    _model.answContent = [NSString stringWithFormat:@"回复 %@：%@", _model.byReviewNick, _model.answContent];
                }
                [arrM addObject:_model];
                userIds = [userIds stringByAppendingString:[NSString stringWithFormat:@",%@",_model.userId]];
            }
            
            
            if (_pageNo == 1) { //顶部刷新
                self.dataSourceQuizAnswList = @[];
            }
            
            NSMutableArray* _tmpData = [NSMutableArray arrayWithArray:self.dataSourceQuizAnswList];
            
            [_tmpData addObjectsFromArray:arrM];
            
            self.dataSourceQuizAnswList = _tmpData;
            
            if ([userIds length] > 0) {
                userIds = [userIds substringFromIndex:1];
            }
            
            finished(YES, codeType, userIds);
            return;
        }
        finished(isSuccessed, codeType, errorMessage);
    }];
}

- (void)requestSaveQuizAnswerByquizId:(NSString*)quizId
                          answContent:(NSString*)answContent
                              creator:(NSString*)creator
                           byReviewId:(NSString*)byReviewId
                         byReviewNick:(NSString*)byReviewNick
                       byReviewUserId:(NSString*)byReviewUserId
                        communityName:(NSString*)communityName
                          communityId:(NSString*)communityId
                        objSendUserId:(NSString*)objSendUserId
                      objPicUrlForMsg:(NSString*)objPicUrlForMsg
                       objTitleForMsg:(NSString*)objTitleForMsg
                             objeType:(NSString*)objeType
                          parentObjId:(NSString*)parentObjId
                           paramsType:(NSInteger)paramsType
                        parentObjType:(NSInteger)parentObjType
                             Finished:(SEGRequestCallBack)finished
{
    NSDictionary* params = @{};
    if (paramsType == 1001) { //byReviewId、byReviewNick不要
        params = @{
                   @"quizId" : quizId,
                   @"answContent" : answContent,
                   @"creator" : creator,
                   //@"byReviewId" : byReviewId,
                   //@"byReviewNick" : byReviewNick,
                   @"byReviewUserId" : byReviewUserId,
                   @"communityName" : communityName,
                   @"communityId" : communityId,
                   //@"objSendUserId" : objSendUserId,
                   @"userId" : kAccountUserID,
                   @"objPicUrlForMsg" : objPicUrlForMsg,
                   @"objTitleForMsg" : objTitleForMsg, //objTitleForMsg,
                   @"objType" : objeType,
                   @"parentObjId" : parentObjId,
                   @"parentObjType" : @(parentObjType)
                   };
    }
    else if (paramsType == 1002) { //byReviewId、byReviewNick不要
        params = @{
                   @"quizId" : quizId,
                   @"answContent" : answContent,
                   @"creator" : creator,
                   @"byReviewId" : byReviewId,
                   //@"byReviewNick" : byReviewNick,
                   @"byReviewUserId" : byReviewUserId,
                   @"communityName" : communityName,
                   @"communityId" : communityId,
                   //@"objSendUserId" : objSendUserId,
                   @"userId" : kAccountUserID,
                   @"objPicUrlForMsg" : objPicUrlForMsg,
                   @"objTitleForMsg" : objTitleForMsg, //objTitleForMsg,
                   @"objType" : objeType,
                   @"parentObjId" : parentObjId,
                   @"parentObjType" : @(parentObjType)
                   };
    }
    
    else {
        params = @{
                   @"quizId" : quizId,
                   @"answContent" : answContent,
                   @"creator" : creator,
                   @"byReviewId" : byReviewId,
                   @"byReviewNick" : byReviewNick,
                   @"byReviewUserId" : byReviewUserId,
                   @"communityName" : communityName,
                   @"communityId" : communityId,
                   //@"objSendUserId" : objSendUserId,
                   @"userId" : kAccountUserID,
                   @"objPicUrlForMsg" : objPicUrlForMsg,
                   @"objTitleForMsg" : objTitleForMsg, //objTitleForMsg,
                   @"objType" : objeType,
                   @"parentObjId" : parentObjId,
                   @"parentObjType" : @(parentObjType)
                   };
    }
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/quiz/quizAnswList.json",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        if (!isSuccessed) {
            finished(NO, codeType, errorMessage);
            return;
        }

        finished(isSuccessed, codeType, errorMessage);
    }];

}

#pragma mark - getter
- (NSArray<SEGFindQuizListByQuizTypeModel*>*)dataSourceFindQuizList
{
    if (!_dataSourceFindQuizList) {
        
        _dataSourceFindQuizList = [NSArray array];
    }
    
    return _dataSourceFindQuizList;
}

- (SEGFindMeQuizListModel*)dataSourceFindMeQuizList
{
    if (!_dataSourceFindMeQuizList) {
        
        _dataSourceFindMeQuizList = [[SEGFindMeQuizListModel alloc] init];
    }
    
    return _dataSourceFindMeQuizList;
}

- (NSArray<SEGQuizAnswListModel*>*)dataSourceQuizAnswList
{
    if (!_dataSourceQuizAnswList) {
        
        _dataSourceQuizAnswList = [NSArray array];
    }
    return _dataSourceQuizAnswList;
}

- (instancetype)init
{
    if (self == [super init]) {
    }
    return self;
}
@end

